$(function () {
    $('.am-header-title').text('我的订单');


    var url = "/h5/orders";
    var user = JSON.parse(localStorage.getItem("user"));
    var account = user.account;
    $.post(url, {
        account: account
    }, function (result) {


        Handlebars.registerHelper('checkPayStatus', function (v1, options) {
            if (v1 == 'n') {
                return options.fn(this);
            } else {
                return options.inverse(this);
            }
        });
        Handlebars.registerHelper('checkStatus', function (v1, options) {
            if (v1 == 'sign') {
                return options.fn(this);
            } else {
                return options.inverse(this);
            }
        });

        var source = $('#order-template').html();
        var template = Handlebars.compile(source);
        var html = template(result);
        $(".data-tab-panel-0").find("div").append(html)
        $("#pageNo").attr("data", parseInt(pageNo) + 1);
        if (result.total > pageNo * 10) {
            $("#more").attr("style", null);
        }
        deleteOrder();
        goPay()
    })


    $(".orderType").click(function () {
        $(".orderType").removeClass("am-active");
        var data = $(this).find("a").attr("data");
        var type = $(this).find("a").attr("type");

        var tabId = $(this).find("a").attr("href");
        tabId = tabId.substr(tabId.indexOf("[") + 1, tabId.lastIndexOf("]") - 1)
        $.post(url, {
            account: account,
            orderType: type,
            orderData: data
        }, function (result) {


            Handlebars.registerHelper('checkPayStatus', function (v1, options) {
                if (v1 == 'n') {
                    return options.fn(this);
                } else {
                    return options.inverse(this);
                }
            });

            var source = $('#order-template').html();
            var template = Handlebars.compile(source);
            var html = template(result);
            $("." + tabId).find("div").empty();
            $("." + tabId).find("div").append(html)
            $("#pageNo").attr("data", parseInt(pageNo) + 1);
            if (result.total > pageNo * 10) {
                $("#more").attr("style", null);
            }
            commentOrder();
            deleteOrder();
            goPay()
        })

    });

    function commentOrder() {
        $(".comment").click(function () {
            var orderId = $(this).attr("data");
            var img = $(this).attr("img");
            var productName = $(this).attr("productName");
            var price = $(this).attr("price");
            var quantity = $(this).attr("quantity");
            var createdate = $(this).attr("createdate");
            var productID = $(this).attr("productID");
            var url = "./order-comment.jsp";
            url=url+"?orderId="+orderId+"&img="+img+"&productName="+productName+"&price="+price+"&quantity="+quantity+"&createdate="+createdate+"&productID="+productID;
            window.location.href = url;
        })

    }

    function deleteOrder() {
        $(".delete").click(function () {
            var orderId = $(this).attr("data");
            var url = "/order/delete"
            $.post(url, {
                id: orderId
            }, function (result) {
                window.location.href = "./order-list.jsp";
            })
        })

    }

    function goPay() {
        $(".goPay").click(function () {
            var orderNo = $(this).attr("orderNo");
            var pid = $(this).attr("pid");
            var orderType = $(this).attr("orderType");
            if (orderType == 1) {
                window.location.href = "../../app/ecshop/pay-order.jsp?orderNo=" + orderNo + "&pid=" + pid + "&from=orderList";
            } else {
                var url = "/h5GoodShop/getGoodShopProductDetail?id=" + pid
                $.post(url, function (result) {
                    window.location.href = "../../app/store/store-pay-order.jsp?orderNo=" + orderNo + "&pid=" + pid + "&id=" + result.id + "&catId=" + result.catId + "&from=orderList";
                })


            }
        })

    }

});

